﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class AddScholarShipData : System.Web.UI.Page
    {
        FundAL fundAL = new FundAL();
        StudentAL studentAL = new StudentAL();
        MemberAL memberAL = new MemberAL();
        public Guid SelectedStudent
        {
            get
            {
                if (Session["StudentId"] == null)
                {
                    return studentAL.GetStudents()[0].StudentId;
                }
                else
                {
                    return new Guid(Session["StudentId"].ToString());
                }
            }
            set
            {
                Session["StudentId"] = value;
            }
        }


        public Guid SelectedScholarShip
        {
            get
            {
                if (Session["ScholarShipId"] == null)
                {
                    return fundAL.GetAllScholarShip()[0].ScholarshipId;
                }
                else
                {
                    return new Guid(Session["ScholarShipId"].ToString());
                }
            }
            set
            {
                Session["ScholarShipId"] = value;
            }
        }

        public Guid SelectedMember
        {
            get
            {
                if (Session["MemberId"] == null)
                {
                    return memberAL.GetAllMembers()[0].MemberId;
                }
                else
                {
                    return new Guid(Session["MemberId"].ToString());
                }
            }
            set
            {
                Session["MemberId"] = value;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            List<StudentDTO> studentDTO = studentAL.GetStudents();
            ddlScholrshipDataStudentName.DataSource = studentDTO;
            ddlScholrshipDataStudentName.DataTextField = "FullName";
            ddlScholrshipDataStudentName.DataValueField = "StudentId";
            ddlScholrshipDataStudentName.SelectedValue = SelectedStudent.ToString();
            ddlScholrshipDataStudentName.DataBind();

            List<ScholarshipDTO> scholarshipDTO = fundAL.GetAllScholarShip();
            ddlScholrshipDataScholrShipName.DataSource = scholarshipDTO;
            ddlScholrshipDataScholrShipName.DataTextField = "Title";
            ddlScholrshipDataScholrShipName.DataValueField = "ScholarshipId";
            ddlScholrshipDataScholrShipName.SelectedValue = SelectedScholarShip.ToString();
            ddlScholrshipDataScholrShipName.DataBind();

            List<MemberDTO> memberDTO = memberAL.GetAllMembers();
            ddlScholarshipAmtSettledBy.DataSource = memberDTO;
            ddlScholarshipAmtSettledBy.DataTextField = "FullName";
            ddlScholarshipAmtSettledBy.DataValueField = "MemberId";
            ddlScholarshipAmtSettledBy.SelectedValue = SelectedMember.ToString();
            ddlScholarshipAmtSettledBy.DataBind();

        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnScholashipDataSubmit_Click(object sender, EventArgs e)
        {

            try
            {
                FundAL fundAL = new FundAL();
                //decimal capital = 0;
                decimal amountPerSlot = 0;


                DateTime acknowledgedAt = DateTime.MinValue;
                DateTime amountSettledAt = DateTime.MinValue;

                if (txtScholrshipDataAmount.Text == "")
                {
                    amountPerSlot = 0;
                }
                else
                {

                    amountPerSlot = Convert.ToDecimal(txtScholrshipDataAmount.Text);
                }

                if (txtScholrshipDataAckOn.Text == "")
                {
                    acknowledgedAt = Convert.ToDateTime("01/01/1800");
                }
                else
                {

                    acknowledgedAt = Convert.ToDateTime(txtScholrshipDataAckOn.Text);
                }

                if (txtScholrshipDataAmtSettledOn.Text == "")
                {
                    amountSettledAt = Convert.ToDateTime("01/01/1800");
                }
                else
                {

                    amountSettledAt = Convert.ToDateTime(txtScholrshipDataAmtSettledOn.Text);
                }
                ScholarshipDataDTO scholarshipDataDTO = new ScholarshipDataDTO
                {
                    ScholarshipDataId = Guid.NewGuid(),
                    ScholarshipId = new Guid(ddlScholrshipDataScholrShipName.SelectedValue),
                    StudentId = new Guid(ddlScholrshipDataStudentName.SelectedValue),
                    amount = amountPerSlot,
                    amountSettledAt = amountSettledAt,
                    AcknowledgeAt = acknowledgedAt,
                    Notes = txtScholarshipDataNotes.Text,
                    SettledBy = new Guid(ddlScholarshipAmtSettledBy.SelectedValue),
                    //ModifiedBy="Tharsan",
                    ModifiedTime = System.DateTime.Now
                };

                string result = fundAL.SaveScholarShipData(scholarshipDataDTO);
                if (result == "Saved")
                {
                    lblAddScholDta.Text = "The Scholarship Data Details added Successfully";
                    txtScholarshipDataNotes.Text = "";
                    txtScholrshipDataAckOn.Text = "";
                    txtScholrshipDataAmount.Text = "";
                    txtScholrshipDataAmtSettledOn.Text = "";
                    


                }
                else if (result == "Updated")
                {
                    lblAddScholDta.Text = "The Scholarship Data Details Updated Successfully";
                    txtScholarshipDataNotes.Text = "";
                    txtScholrshipDataAckOn.Text = "";
                    txtScholrshipDataAmount.Text = "";
                    txtScholrshipDataAmtSettledOn.Text = "";
                }
                else if (result == "AlreadyExists")
                {
                    lblAddScholDta.Text = "The Scholarship Data is already exists ";
                    txtScholarshipDataNotes.Text = "";
                    txtScholrshipDataAckOn.Text = "";
                    txtScholrshipDataAmount.Text = "";
                    txtScholrshipDataAmtSettledOn.Text = "";
                }
                else
                {
                    lblAddScholDta.Text = "Your data cannot be added";
                    txtScholarshipDataNotes.Text = "";
                    txtScholrshipDataAckOn.Text = "";
                    txtScholrshipDataAmount.Text = "";
                    txtScholrshipDataAmtSettledOn.Text = "";
                }
            }
            catch (Exception ee)
            {
                lblAddScholDta.Text = "There is an exception occured while inserting the Scholarship Data details " + ee.ToString();
                txtScholarshipDataNotes.Text = "";
                txtScholrshipDataAckOn.Text = "";
                txtScholrshipDataAmount.Text = "";
                txtScholrshipDataAmtSettledOn.Text = "";
            }

        }

        protected void ddlScholrshipDataScholrShipName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedScholarShip = new Guid(ddlScholrshipDataScholrShipName.SelectedValue);
        }

        protected void ddlScholrshipDataStudentName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedStudent = new Guid(ddlScholrshipDataStudentName.SelectedValue);
        }

        protected void ddlScholarshipAmtSettledBy_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
